package com.unknowpupil.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unknowpupil.web.base.impl.BaseCrudServiceImpl;
import com.unknowpupil.web.domain.JobAndTrigger;
import com.unknowpupil.web.mapper.JobAndTriggerMapper;
import com.unknowpupil.web.service.IJobAndTriggerService;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @Classname: JobAndTriggerServiceImpl
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/10/20 3:33
 * @Description:
 * @Version: 1.0
 */
@Service
public class JobAndTriggerServiceImpl extends BaseCrudServiceImpl<JobAndTrigger, JobAndTriggerMapper> implements IJobAndTriggerService {
    @Resource
    private  JobAndTriggerMapper jobAndTriggerMapper;
    @Autowired
    private Scheduler scheduler;
    @Override
    public PageInfo<JobAndTrigger> getJobAndTriggerDetails(int pageNum, int pageSize) throws SchedulerException {
        PageHelper.startPage(pageNum, pageSize);
        List<JobAndTrigger> list = jobAndTriggerMapper.getJobAndTriggerDetails();
        Iterator<JobAndTrigger> listIt = list.iterator();
        while (listIt.hasNext()) {
            JobAndTrigger jobAndTrigger = listIt.next();
            Trigger.TriggerState triggerState = scheduler.getTriggerState(new TriggerKey(jobAndTrigger.getTriggerName(), jobAndTrigger.getTriggerGroup()));
            if(triggerState == Trigger.TriggerState.NORMAL){
                jobAndTrigger.setRunning(true);
            }
        }
        PageInfo<JobAndTrigger> page = new PageInfo<JobAndTrigger>(list);
        return page;
    }
}
